import { UserInfo } from '@/service/index/type'
import { defineStore } from 'pinia'

export interface UserState {
  userInfo: UserInfo | null
  token: string
}

export default defineStore('user', {
  state: (): UserState => ({
    userInfo: null,
    token: '',
  }),
  getters: {
    isLogin() {
      return this.token && !!this.userInfo
    },
  },
  actions: {
    updateToken(val: string) {
      this.token = val
    },
    updateUserInfo(user: UserInfo) {
      this.userInfo = user
    },
    clearInfo() {
      this.userInfo = null
      this.token = ''
    },
  },
  persist: true,
})
